Return to start page

Core/General/Struct Vector.j

Code

		
1			library AStructCoreGeneralVector requires optional ALibraryCoreDebugMisc
2
3 /// @author Tamino Dauth
4 /// @source http://www.cplusplus.com/reference/stl/list/
5 //! textmacro A_VECTOR takes STRUCTPREFIX, NAME, ELEMENTTYPE, NULLVALUE, MAXSIZE, STRUCTSPACE
6
7 /// @todo Should be a part of @struct $NAME$, vJass bug.
8 $STRUCTPREFIX$ function interface $NAME$UnaryPredicate takes $ELEMENTTYPE$ value returns boolean
9
10 /// @todo Should be a part of @struct $NAME$, vJass bug.
11 $STRUCTPREFIX$ function interface $NAME$BinaryPredicate takes $ELEMENTTYPE$ value0, $ELEMENTTYPE$ value1 returns boolean
12
13 /// @todo Should be a part of @struct $NAME$, vJass bug.
14 $STRUCTPREFIX$ function interface $NAME$UnaryFunction takes $ELEMENTTYPE$ element returns nothing //Rückgabewert wurde vorerst rausgenommen, bis ich weiß, was er bringt
15
16 /// Generator.
17 /// Allows filling some elements with the return value.
18 /// @todo Should be a part of @struct $NAME$, vJass bug.
19 $STRUCTPREFIX$ function interface $NAME$Generator takes nothing returns $ELEMENTTYPE$
20
21 /// @todo Should be a part of @struct $NAME$, vJass bug.
22 $STRUCTPREFIX$ function interface $NAME$BinaryOperation takes $ELEMENTTYPE$ value0, $ELEMENTTYPE$ value1 returns $ELEMENTTYPE$
23
24 $STRUCTPREFIX$ struct $NAME$[$STRUCTSPACE$]
25 //members
26 private $ELEMENTTYPE$ array m_element[$MAXSIZE$]
27 private integer m_size
28 //Quicksort statics
29 private $ELEMENTTYPE$ m
30 private integer j
31
32 public method size takes nothing returns integer
33 return this.m_size
34 endmethod
35
36 /**
37 * @return Returns the first element value of vector.
38 */
39 public method front takes nothing returns $ELEMENTTYPE$
40 return this.m_element[0]
41 endmethod
42
43 /**
44 * @return Returns the last element value of vector.
45 */
46 public method back takes nothing returns $ELEMENTTYPE$
47 return this.m_element[this.m_size - 1]
48 endmethod
49
50 /**
51 * @return Returns the last element index of vector.
52 */
53 public method backIndex takes nothing returns integer
54 return this.m_size - 1
55 endmethod
56
57 /**
58 * @return Returns the element at index @param index.
59 */
60 public method at takes integer index returns $ELEMENTTYPE$
61 debug if (index < 0 or index >= this.m_size) then
62 debug call Print("Invalid index: " + I2S(index) + ".")
63 debug return $NULLVALUE$
64 debug endif
65 return this.m_element[index]
66 endmethod
67
68 public method empty takes nothing returns boolean
69 return this.m_size == 0
70 endmethod
71
72 public method resize takes integer size, $ELEMENTTYPE$ newContent returns nothing
73 local integer i
74 if (size < this.m_size) then
75 set i = this.m_size
76 loop
77 exitwhen (i == size)
78 call this.popBack()
79 set i = i - 1
80 endloop
81 elseif (size > this.m_size) then
82 set i = size
83 loop
84 exitwhen (i == this.m_size)
85 call this.pushBack(newContent)
86 set i = i + 1
87 endloop
88 //else
89 //do nothing
90 endif
91 endmethod
92
93 /// Assigns new content to the container, dropping all the elements contained in the container object before the call and replacing them by those specified by the parameters:
94 public method assign takes integer number, $ELEMENTTYPE$ content returns nothing
95 //push back number times with content content
96 local integer i = 0
97 call this.clear()
98 loop
99 exitwhen (i == number)
100 call this.pushBack(content)
101 set i = i + 1
102 endloop
103 endmethod
104
105 /**
106 * Inserts a new element at the beginning of the list, right before its current first element. The content of this new element is initialized to @param value.
107 * This effectively increases the list size by one.
108 */
109 public method pushFront takes $ELEMENTTYPE$ value returns nothing
110 call this.insert(0, value)
111 endmethod
112
113 /**
114 * Removes the first element in the list container, effectively reducing the list size by one.
115 * This doesn't call the removed element's destructor!
116 */
117 public method popFront takes nothing returns nothing
118 call this.erase(0)
119 endmethod
120
121 /**
122 * Adds a new element at the end of the list, right after its current last
123 * element. The content of this new element is initialized to @param value.
124 * This effectively increases the list size by one.
125 */
126 public method pushBack takes $ELEMENTTYPE$ value returns nothing
127 set this.m_element[this.m_size] = value
128 set this.m_size = this.m_size + 1
129 endmethod
130
131 /**
132 * Removes the last element in the list container, effectively reducing the list size by one.
133 * This calls the removed element's destructor.
134 */
135 public method popBack takes nothing returns nothing
136 set this.m_element[this.m_size - 1] = $NULLVALUE$
137 set this.m_size = this.m_size - 1
138 endmethod
139
140 /**
141 * The list container is extended by inserting new elements before the element at position @param position with value @param value.
142 * This effectively increases the container size by @param number.
143 */
144 public method insertNumber takes integer position, integer number, $ELEMENTTYPE$ value returns nothing
145 local integer i = this.m_size - 1
146 local integer firstExitValue = position + number - 1
147 local integer secondExitValue = position + number
148 debug if (position < 0 or position >= this.m_size) then
149 debug call Print("Wrong position: " + I2S(position) + ".")
150 debug return
151 debug elseif (number <= 0 or position + number > this.m_size) then
152 debug call Print("Wrong number: " + I2S(number) + ".")
153 debug return
154 debug elseif (this.m_size + number > thistype.maxSize()) then
155 debug call Print("Size would be too high: " + I2S(this.m_size + number) + ". Maximum size is: " + I2S(thistype.maxSize()) + ".")
156 debug return
157 debug endif
158 loop
159 exitwhen (i == firstExitValue)
160 set this.m_element[i + number] = this.m_element[i]
161 set i = i - 1
162 endloop
163 set i = position
164 loop
165 exitwhen (i == secondExitValue)
166 set this.m_element[i] = value
167 set i = i + 1
168 endloop
169 set this.m_size = this.m_size + number
170 endmethod
171
172 public method insert takes integer position, $ELEMENTTYPE$ value returns nothing
173 call this.insertNumber(position, 1, value)
174 endmethod
175
176 /**
177 * Removes from the list @param number elements at position @param position.
178 * This effectively reduces the list size by @param number.
179 */
180 public method eraseNumber takes integer position, integer number returns nothing
181 local integer i = position + number
182 debug if (position < 0 or position >= this.m_size) then
183 debug call Print("Wrong position: " + I2S(position) + ".")
184 debug return
185 debug elseif (number <= 0 or position + number > this.m_size) then
186 debug call Print("Wrong number: " + I2S(number) + ".")
187 debug return
188 debug endif
189 loop
190 exitwhen (i == this.m_size)
191 set this.m_element[i - number] = this.m_element[i]
192 set this.m_element[i] = $NULLVALUE$ //clear
193 set i = i + 1
194 endloop
195 set this.m_size = this.m_size - number
196 endmethod
197
198 public method erase takes integer position returns nothing
199 call this.eraseNumber(position, 1)
200 endmethod
201
202 /**
203 * Exchanges the content of the vector by the content of @param vector, which is another vector object containing elements of the same type. Sizes may differ.
204 * After the call to this member function, the elements in this container are those which were in @param vector before the call, and the elements of @param vector are those which were in this.
205 */
206 public method swap takes thistype vector returns nothing
207 local $ELEMENTTYPE$ tempValue
208 local integer i = 0
209 debug if (this == vector) then
210 debug call Print("Same vector.")
211 debug return
212 debug endif
213 loop
214 exitwhen (i == this.m_size or i == vector.m_size)
215 set tempValue = this.m_element[i]
216 set this.m_element[i] = vector.m_element[i]
217 set vector.m_element[i] = tempValue
218 set i = i + 1
219 endloop
220 endmethod
221
222 /// All the elements in the vector container are dropped: they are removed from the vector container, leaving it with a size of 0.
223 public method clear takes nothing returns nothing
224 local integer i = 0
225 loop
226 exitwhen (i == this.m_size)
227 set this.m_element[i] = $NULLVALUE$
228 set i = i + 1
229 endloop
230 set this.m_size = 0
231 endmethod
232
233 /**
234 * Moves @param vectorNumber elements from vector @param vector at position @param vectorPosition into the vector container at the specified position @param position, effectively inserting the specified elements into the container and removing them from @param vector.
235 * This increases the container size by the amount of elements inserted, and reduces the size of @param vector by the same amount.
236 */
237 public method splice takes integer position, thistype vector, integer vectorPosition, integer vectorNumber returns nothing
238 local integer i = vectorPosition
239 local integer exitValue = vectorPosition + vectorNumber
240 local integer secondIndex
241 debug if (position < 0 or position >= this.m_size) then
242 debug call Print("Wrong position: " + I2S(position) + ".")
243 debug return
244 debug elseif (this == vector) then
245 debug call Print("Same vector.")
246 debug return
247 debug elseif (vectorPosition < 0 or vectorPosition >= vector.m_size) then
248 debug call Print("Wrong vector position: " + I2S(vectorPosition) + ".")
249 debug return
250 debug elseif (vectorNumber <= 0 or vectorNumber + vectorPosition > vector.m_size) then
251 debug call Print("Wrong vector number: " + I2S(vectorNumber) + ".")
252 debug return
253 debug endif
254 loop
255 exitwhen (i == exitValue)
256 set secondIndex = i + position
257 if (secondIndex >= this.m_size) then
258 call this.pushBack(vector.m_element[i])
259 else
260 call this.insert(secondIndex, vector.m_element[i])
261 endif
262 set i = i + 1
263 endloop
264 call vector.eraseNumber(vectorPosition, vectorNumber)
265 endmethod
266
267 /**
268 * Removes from the list all the elements with the specific value @param value. This reduces the list size by the amount of elements removed.
269 * Unlike method @method erase, which erases elements by their position, this method removes elements by their value.
270 * A similar method, @method removeIf, exists, which allows for a condition other than a plain value comparison to be performed on each element in order to determine the elements to be removed.
271 */
272 public method remove takes $ELEMENTTYPE$ value returns nothing
273 //backwards should be faster
274 local integer i = this.m_size - 1
275 loop
276 exitwhen (i < 0)
277 if (this.m_element[i] == value) then
278 call this.erase(i)
279 endif
280 set i = i - 1
281 endloop
282 endmethod
283
284 /**
285 * Removes from the list all the elements for which predicate @param unaryPredicate returns true. This reduces the list size by the amount of elements removed.
286 * Predicate @param unaryPredicate can be implemented as a function taking one argument of the same type as the list and returning a @type boolean.
287 * The function calls unaryPredicate.evaluate(x) for each element (where i is the element). Any of the elements in the list for which this returns true, is removed from the container.
288 */
289 public method removeIf takes $NAME$UnaryPredicate unaryPredicate returns nothing
290 //backwards should be faster
291 local integer i = this.m_size - 1
292 debug if (unaryPredicate == 0) then
293 debug call Print("Unary predicate is 0.")
294 debug return
295 debug endif
296 loop
297 exitwhen (i < 0)
298 if (unaryPredicate.evaluate(this.m_element[i])) then
299 call this.erase(i)
300 endif
301 set i = i - 1
302 endloop
303 endmethod
304
305 /**
306 * The first version, with no parameters, removes all but the first element from every consecutive group of equal elements in the vector container.
307 * Notice that an element is only removed from the vector if it is equal to the element immediately preceding it. Thus, this function is specially useful for sorted lists.
308 */
309 public method unique takes nothing returns nothing
310 //backwards should be faster
311 local integer i = this.m_size - 2
312 loop
313 exitwhen (i < 0)
314 if (this.m_element[i] == this.m_element[i + 1]) then
315 call this.erase(i + 1) //remove rear value
316 endif
317 set i = i - 1
318 endloop
319 endmethod
320
321 /// For the second version, accepting a binary predicate, a specific comparison function to determine the "uniqueness" of an element can be specified. In fact, any behavior can be implemented (and not only a plain comparison), but notice that the function will call binaryPredicate.evaluate(x, x - 1)) for all pairs of elements (where x is an element) and remove x from the list if the predicate returns true.
322 public method uniqueIf takes $NAME$BinaryPredicate binaryPredicate returns nothing
323 //backwards should be faster
324 local integer i = this.m_size - 2
325 debug if (binaryPredicate == 0) then
326 debug call Print("Binary predicate is 0.")
327 debug return
328 debug endif
329 loop
330 exitwhen (i < 0)
331 if (binaryPredicate.evaluate(this.m_element[i + 1], this.m_element[i])) then
332 call this.erase(i + 1) //remove rear value
333 endif
334 set i = i - 1
335 endloop
336 endmethod
337
338 /**
339 * Merges @param vector into the vector, inserting all the elements of @param vector into the vector object at their respective ordered positions. This empties @param vector and increases the vector size.
340 * @todo insert sorted?!
341 */
342 public method merge takes thistype vector returns nothing
343 local integer i = 0
344 call this.insertNumber(0, vector.m_size, $NULLVALUE$)
345 loop
346 exitwhen (i == vector.m_size)
347 set this.m_element[i] = vector.m_element[i]
348 set i = i + 1
349 endloop
350 call vector.clear()
351 endmethod
352
353 /**
354 * The second version (template function), has the same behavior, but takes a specific function to perform the comparison operation in charge of determining the insertion points. The comparison function has to perform weak strict ordering (which basically means the comparison operation has to be transitive and irreflexive).
355 * The merging is performed using two iterators: one to iterate through x and another one to keep the insertion point in the vector object; During the iteration of x, if the current element in x compares less than the element at the current insertion point in the list object, the element is removed from x and inserted into that location, otherwise the insertion point is advanced. This operation is repeated until either end is reached, in which moment the remaining elements of x (if any) are moved to the end of the list object and the function returns (this last operation is performed in constant time).
356 * @todo implement mergeIf pls.
357 * template
358 * void merge ( list& x, Compare comp );
359 */
360
361 /// Common quick sort algorithm.
362 private method quickSort takes integer left, integer right, $NAME$BinaryPredicate binaryPredicate returns nothing
363 local integer i
364 local $ELEMENTTYPE$ temp
365 if (right > left) then
366 set this.m = this.m_element[right]
367 set i = left - 1
368 set this.j = right
369 loop
370 loop
371 set i = i + 1
372 exitwhen (not binaryPredicate.evaluate(this.m_element[i], this.m))
373 endloop
374
375 loop
376 set this.j = this.j - 1
377 exitwhen (binaryPredicate.evaluate(this.m_element[this.j], this.m))
378 endloop
379
380 exitwhen (i >= this.j)
381 set temp = this.m_element[i]
382 set this.m_element[i] = this.m_element[this.j]
383 set this.m_element[this.j] = temp
384 endloop
385
386 set temp = this.m_element[i]
387 set this.m_element[i] = this.m_element[right]
388 set this.m_element[right] = temp
389
390 call this.quickSort(left, i - 1, binaryPredicate)
391 call this.quickSort(i + 1, right, binaryPredicate)
392 endif
393 endmethod
394
395 /**
396 * Sorts the elements in the container from lower to higher. The sorting is performed by comparing the elements in the container in pairs using a sorting algorithm.
397 * The comparisons are perfomed using function @param binaryPredicate, which performs weak strict ordering (this basically means the comparison operation has to be transitive and irreflexive).
398 */
399 public method sortNumber takes integer position, integer number, $NAME$BinaryPredicate binaryPredicate returns nothing
400 debug if (not this.debugCheckPositionAndNumber(position, number)) then
401 debug return
402 debug endif
403 call this.quickSort(position, position + number - 1, binaryPredicate)
404 endmethod
405
406 public method sort takes $NAME$BinaryPredicate binaryPredicate returns nothing
407 call this.sortNumber(0, this.m_size, binaryPredicate)
408 endmethod
409
410 /// Reverses the order of the elements in the list container.
411 /// @todo I'm not sure if this is best solution.
412 public method reverseNumber takes integer position, integer number returns nothing
413 local integer i = position
414 local integer exitValue = position + (number / 2)
415 local $ELEMENTTYPE$ temp
416 local integer swapindex
417 debug if (not this.debugCheckPositionAndNumber(position, number)) then
418 debug return
419 debug endif
420 loop
421 exitwhen (i == exitValue)
422 set swapindex = ((2 * position) + number - i - 1)
423 set temp = this.m_element[i]
424 set this.m_element[i] = this.m_element[swapindex]
425 set this.m_element[swapindex] = temp
426 set i = i + 1
427 endloop
428 endmethod
429
430 public method reverse takes nothing returns nothing
431 call this.reverseNumber(0, this.m_size)
432 endmethod
433
434 /// @todo Implement the following methods:
435 /// adjacentFind
436 /// adjacentFindIf
437 /// binarySearch
438
439 /**
440 * Copies elements in range @param position0, @param number0 into vector @param vector
441 * starting at index @param position1.
442 * @return Returns index of last copied element.
443 */
444 public method copyNumber takes integer position0, integer number0, $NAME$ vector, integer position1 returns integer
445 local integer exitValue = position0 + number0
446 debug call this.debugCheckPositionAndNumber(position0, number0)
447 debug call vector.debugCheckPositionAndNumber(position1, number0)
448 loop
449 exitwhen (position0 == exitValue)
450 set vector.m_element[position1] = this.m_element[position0]
451 set position0 = position0 + 1
452 set position1 = position1 + 1
453 endloop
454 return exitValue - 1
455 endmethod
456
457 /**
458 * Copies all elements into vector @param vector.
459 * Vectors should have the same size.
460 * @return Returns index of last copied element.
461 */
462 public method copy takes $NAME$ vector returns integer
463 return this.copyNumber(0, this.m_size, vector, 0)
464 endmethod
465
466 /// copyBackward
467 /// copyN
468 /// countIf
469 /// equal
470 /// equalIf
471 /// fill
472 /// fillN
473
474 public method findNumber takes integer position, integer number, $ELEMENTTYPE$ value returns integer
475 local integer i = position
476 local integer exitValue = position + number
477 debug call this.debugCheckPositionAndNumber(position, number)
478 loop
479 exitwhen (i == exitValue)
480 if (this.m_element[i] == value) then
481 return i
482 endif
483 set i = i + 1
484 endloop
485 return -1
486 endmethod
487
488 public method find takes $ELEMENTTYPE$ value returns integer
489 return this.findNumber(0, this.m_size, value)
490 endmethod
491
492 public method findEndNumber takes integer position, integer number, $ELEMENTTYPE$ value returns integer
493 local integer i = position + number - 1
494 local integer exitValue = position - 1
495 debug call this.debugCheckPositionAndNumber(position, number)
496 loop
497 exitwhen (i == exitValue)
498 if (this.m_element[i] == value) then
499 return i
500 endif
501 set i = i - 1
502 endloop
503 return -1
504 endmethod
505
506 public method findEnd takes $ELEMENTTYPE$ value returns integer
507 return this.findEndNumber(0, this.m_size, value)
508 endmethod
509
510 /// findFirstOf
511
512 public method findIfNumber takes integer position, integer number, $NAME$UnaryPredicate unaryPredicate returns integer
513 local integer i = position
514 local integer exitValue = position + number
515 debug call this.debugCheckPositionAndNumber(position, number)
516 loop
517 exitwhen (i == exitValue)
518 if (unaryPredicate.evaluate(this.m_element[i])) then
519 return i
520 endif
521 set i = i + 1
522 endloop
523 return -1
524 endmethod
525
526 public method findIf takes $NAME$UnaryPredicate unaryPredicate returns integer
527 return this.findIfNumber(0, this.m_size, unaryPredicate)
528 endmethod
529
530 public method contains takes $ELEMENTTYPE$ value returns boolean
531 return this.find(value) != -1
532 endmethod
533
534 /**
535 * Calls user-defined function @param unaryFunction for each element in range beginning at position @param position and ending at position @param position + @param number - 1.
536 * Function @param unaryFunction is called like this: call unaryFunction.execute(x) where x is the current element of iteration.
537 */
538 public method forEachNumber takes integer position, integer number, $NAME$UnaryFunction unaryFunction returns nothing
539 local integer i = position
540 local integer exitValue = position + number
541 debug if (not this.debugCheckPositionAndNumber(position, number)) then
542 debug return
543 debug endif
544 loop
545 exitwhen (i == exitValue)
546 call unaryFunction.execute(this.m_element[i])
547 set i = i + 1
548 endloop
549 endmethod
550
551 public method forEach takes $NAME$UnaryFunction unaryFunction returns nothing
552 call this.forEachNumber(0, this.m_size, unaryFunction)
553 endmethod
554
555 /// Füllt einen Elementebereich mit dem Rückgabewert einer benutzerdefinierten Funktion.
556 public method generateNumber takes integer position, integer number, $NAME$Generator generator returns nothing
557 local integer i = position
558 local integer exitValue = position + number
559 debug if (not this.debugCheckPositionAndNumber(position, number)) then
560 debug return
561 debug endif
562 loop
563 exitwhen (i == exitValue)
564 set this.m_element[i] = generator.evaluate()
565 set i = i + 1
566 endloop
567 endmethod
568
569 public method generate takes $NAME$Generator generator returns nothing
570 call this.generateNumber(0, this.m_size, generator)
571 endmethod
572
573 /**
574 * Returns true if all the elements in the range @param position1, @param number1
575 * are equivalent to some element in @param position0, @param number0.
576 * The comparison uses @param comparator to test this;
577 * The value of an element, a, is equivalent to another one, b, when (not
578 * comparator.evaluate(a, b) and not comparator(b, a)).
579 * For the function to yield the expected result, the elements in the ranges shall
580 * be already ordered according to the same strict weak ordering criterion
581 * (@param comparator).
582 * C++ template example:
583 * @code
584 * bool includes ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 )
585 * {
586 * while (first1!=last1)
587 * {
588 * if (*first2<*first1) break;
589 * else if (*first1<*first2) ++first1;
590 * else { ++first1; ++first2; }
591 * if (first2==last2) return true;
592 * }
593 * return false;
594 * }
595 * @endcode
596 * @return Returns true if every element in the range @param position1, @param number1 is contained in the range @param position0, @param number0, false otherwise.
597 */
598 public method includes takes integer position0, integer number0, integer position1, integer number1, $NAME$BinaryPredicate comparator returns boolean
599 local integer lastIndex0 = position0 + number0 - 1
600 local integer lastIndex1 = position1 + number1 - 1
601 debug if (position0 < 0 or position0 >= this.m_size) then
602 debug call Print("Wrong position0: " + I2S(position0) + ".")
603 debug return false
604 debug elseif (number0 <= 0 or position0 + number0 > this.m_size) then
605 debug call Print("Wrong number0: " + I2S(number0) + ".")
606 debug return false
607 debug elseif (position1 < 0 or position1 >= this.m_size) then
608 debug call Print("Wrong position1: " + I2S(position1) + ".")
609 debug return false
610 debug elseif (number1 <= 0 or position1 + number1 > this.m_size) then
611 debug call Print("Wrong number1: " + I2S(number1) + ".")
612 debug return false
613 debug endif
614 loop
615 exitwhen (position0 == lastIndex0)
616 if (comparator.evaluate(this.m_element[position0], this.m_element[position1])) then
617 exitwhen (true)
618 elseif (comparator.evaluate(this.m_element[position1], this.m_element[position0])) then
619 set position0 = position0 + 1
620 else
621 set position0 = position0 + 1
622 set position1 = position1 + 1
623 endif
624 if (position1 == lastIndex1) then
625 return true
626 endif
627 endloop
628 return false
629 endmethod
630
631
632 /**
633 * Compute cumulative inner product of range
634 *
635 * Returns the result of accumulating @param initialValue with the inner products of the
636 * pairs formed by the elements of two ranges starting at @param position0 and @param position1.
637 * The two default operations (to add up the result of multiplying the pairs) have * to be overridden by parameters @param binaryOperation0 and @param binaryOperation1.
638 * @return The result of accumulating @param initialValue and the products of all the pairs of elements in the ranges starting at @param position0 and @param position1.
639 *
640 * C++ template example:
641 * @code
642 * template
643 * T inner_product ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init )
644 * {
645 * while (first1!=last1)
646 * init = init + (*first1++)*(*first2++);
647 * // or: init=binary_op1(init,binary_op2(*first1++,*first2++))
648 * // for the binary_op's version
649 * return init;
650 * }
651 * @endcode
652 */
653 public method innerProduct takes integer position0, integer number0, integer position1, $ELEMENTTYPE$ initialValue, $NAME$BinaryOperation binaryOperation0, $NAME$BinaryOperation binaryOperation1 returns $ELEMENTTYPE$
654 local integer lastIndex = position0 + number0 - 1
655 debug if (position0 < 0 or position0 >= this.m_size) then
656 debug call Print("Wrong position0: " + I2S(position0) + ".")
657 debug return $NULLVALUE$
658 debug elseif (number0 <= 0 or position0 + number0 > this.m_size) then
659 debug call Print("Wrong number0: " + I2S(number0) + ".")
660 debug return $NULLVALUE$
661 debug elseif (position1 < 0 or position1 >= this.m_size) then
662 debug call Print("Wrong position1: " + I2S(position1) + ".")
663 debug return $NULLVALUE$
664 debug elseif (binaryOperation0 == 0) then
665 debug call Print("Binary operation 0 is 0.")
666 debug return $NULLVALUE$
667 debug elseif (binaryOperation1 == 0) then
668 debug call Print("Binary operation 1 is 0.")
669 debug return $NULLVALUE$
670 debug endif
671 loop
672 exitwhen (position0 == lastIndex)
673 set initialValue = binaryOperation0.evaluate(initialValue, binaryOperation1.evaluate(this.m_element[position0], this.m_element[position1]))
674 set position0 = position0 + 1
675 set position1 = position1 + 1
676 endloop
677 return initialValue
678 endmethod
679
680 /// inplaceMerge
681 /// maxElement
682 /// minElement
683
684 public method operator[] takes integer index returns $ELEMENTTYPE$
685 debug if (index < 0 or index >= this.m_size) then
686 debug call Print("Invalid index: " + I2S(index) + ".")
687 debug return $NULLVALUE$
688 debug endif
689 return this.m_element[index]
690 endmethod
691
692 public method operator[]= takes integer index, $ELEMENTTYPE$ value returns nothing
693 debug if (index < 0 or index >= this.m_size) then
694 debug call Print("Invalid index: " + I2S(index) + ".")
695 debug return
696 debug endif
697 set this.m_element[index] = value
698 endmethod
699
700 public method operator< takes thistype vector returns boolean
701 debug if (this == vector) then
702 debug call Print("Same vector.")
703 debug endif
704 return this.m_size < vector.m_size
705 endmethod
706
707 debug private method debugCheckPositionAndNumber takes integer position, integer number returns boolean
708 debug if (position < 0 or position >= this.m_size) then
709 debug call Print("Wrong position: " + I2S(position) + ".")
710 debug return false
711 debug elseif (number <= 0 or position + number > this.m_size) then
712 debug call Print("Wrong number: " + I2S(number) + ".")
713 debug return false
714 debug endif
715 debug return true
716 debug endmethod
717
718 public static method create takes nothing returns thistype
719 local thistype this = thistype.allocate()
720 //members
721 set this.m_size = 0
722 return this
723 endmethod
724
725 public static method createWithSize takes integer size, $ELEMENTTYPE$ content returns thistype
726 local thistype this = thistype.allocate()
727 call this.resize(size, content)
728 return this
729 endmethod
730
731 /// Creates a vector by filling it with elements of vector @param other.
732 public static method createByOther takes thistype other returns thistype
733 local thistype this = thistype.allocate()
734 local integer i = 0
735 loop
736 exitwhen (i == other.m_size)
737 call this.pushBack(other.m_element[i])
738 set i = i + 1
739 endloop
740 return this
741 endmethod
742
743 /// Vector will be cleared before destruction.
744 public method onDestroy takes nothing returns nothing
745 call this.clear()
746 endmethod
747
748 public static constant method maxSize takes nothing returns integer
749 return $MAXSIZE$
750 endmethod
751
752 public static constant method maxInstances takes nothing returns integer
753 return $STRUCTSPACE$ / $MAXSIZE$
754 endmethod
755 endstruct
756 //! endtextmacro
757
758 /**
759 * default vectors, Jass data types
760 * max instances = required struct space / biggest array member size
761 * 400000 is struct space maximum
762 * max instances = 50000 / 100 = 500
763 */
764 //! runtextmacro A_VECTOR("", "AIntegerVector", "integer", "0", "100", "50000")
765 //! runtextmacro A_VECTOR("", "AStringVector", "string", "null", "100", "50000")
766 //! runtextmacro A_VECTOR("", "ABooleanVector", "boolean", "false", "100", "50000")
767 //! runtextmacro A_VECTOR("", "ARealVector", "real", "0.0", "100", "50000")
768 //! runtextmacro A_VECTOR("", "AHandleVector", "handle", "null", "100", "50000")
769
770 //! runtextmacro A_VECTOR("", "AEffectVector", "effect", "null", "100", "50000")
771 //! runtextmacro A_VECTOR("", "AUnitVector", "unit", "null", "100", "50000")
772 //! runtextmacro A_VECTOR("", "AItemVector", "item", "null", "100", "50000")
773 //! runtextmacro A_VECTOR("", "ADestructableVector", "destructable", "null", "100", "50000")
774 //! runtextmacro A_VECTOR("", "ARectVector", "rect", "null", "100", "50000")
775 //! runtextmacro A_VECTOR("", "AWeatherEffectVector", "weathereffect", "null", "100", "50000")
776
777 // use AIntegerVector for struct types.
778 endlibrary